package com.example.Cairodio;

import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 
 * @author Shokry and Monica
 * 
 * 
 *         This Adapter is used for both NearbyPlacesActivity and AudioClips
 *         Activity. There are 2 constructors depending on which activity uses
 *         this class.
 * 
 */

public class ListAdapter extends BaseAdapter {
	private Context mContext;
	private String[] mTexts;

	public ListAdapter(Context c, int numClips) {
		mContext = c;
		mTexts = new String[numClips];
		for (int i = 0; i < numClips; i++) {
			mTexts[i] = c.getText(R.string.audioclips_activity_clip) + " "
					+ (i + 1);
		}
	}

	public ListAdapter(Context c, Vector<String> places) {
		mContext = c;
		mTexts = new String[places.size()];

		for (int i = 0; i < places.size(); i++) {
			String placeName = getValue(places.get(i));
			mTexts[i] = placeName;
		}
	}

	/**
	 * @author Shokry
	 * 
	 *         This method gets the name of the place from the string.xml file.
	 *         Thus, it depends on the locale of the App. there are 2 String
	 *         arrays used. The first in english, is used to get the index of
	 *         place name. This index is used to get the value of the name in
	 *         the current language. This method was used because of the unity
	 *         of our database (EN)
	 * 
	 * @param place
	 * @return
	 */
	private String getValue(String place) {

		String[] allPlaces = mContext.getResources().getStringArray(
				R.array.places_index);

		int index = 0;
		boolean found = false;

		for (; index < allPlaces.length; index++) {
			if (place.equals(allPlaces[index])) {
				found = true;
				break;
			}
		}

		if (!found)
			return place;

		// Translate
		String[] allPlacesNames = mContext.getResources().getStringArray(
				R.array.places_values);
		return allPlacesNames[index];

	}

	public int getCount() {
		return mTexts.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new ImageView for each item rerefenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {

		View row;
		// if (convertView == null) { // if it's not recycled, initialize some
		// attributes

		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		row = inflater.inflate(R.layout.clip_item, parent, false);
		TextView label = (TextView) row.findViewById(R.id.clipName);
		label.setText(mTexts[position]);

		// } else {
		// row = (View) convertView;
		// }

		return row;
	}

}
